using System;
using System.Collections.Generic;
using System.Linq;
using GameFrameworkLibraly.BaseObjects;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;

namespace GameFrameworkLibraly.Controls
{
    public class SubmenuControl:Control
    {
        public Control ExitControl { get; private set; }
        public List<Control> Controls { get; private set; }
        public SubmenuControl ParentMenu { get; private set; }

        /// <summary>
        /// Occures when the submenu is closed
        /// </summary>
        public event EventHandler SubMenuClosed;


        /// <summary>
        /// Creates a new instance of submenu
        /// </summary>
        /// <param name="game">Current game</param>
        /// <param name="controls">list of controls that will be displayed in this submenu</param>
        /// <param name="parentMenu">Parent submenu</param>
        /// <param name="exitControl">Control that will call closing of this submenu when it will be releazed</param>
        public SubmenuControl(MyGame game, List<Control> controls, SubmenuControl parentMenu, Control exitControl):base(game)
        {
            Controls = controls;
            ParentMenu = parentMenu;
            ExitControl = exitControl;
            ExitControl.Released += ExitControlReleased;
        }

        private void ExitControlReleased(object sender, EventArgs e)
        {
            InvokeSubMenuClosed(EventArgs.Empty);
        }

        private void InvokeSubMenuClosed(EventArgs e)
        {
            EventHandler handler = SubMenuClosed;
            if (handler != null) handler(this, e);
        }

        #region Control
        /// <summary>
        /// Draws the control
        /// </summary>
        /// <param name="spriteBatch"></param>
        public override void Draw(SpriteBatch spriteBatch)
        {
            if(!Visible)
                return;
            if (BackgroundTexture != null)
                spriteBatch.Draw(BackgroundTexture, DestinationRectangle, BackColorFilter);
            foreach (var control in Controls)
            {
                control.Draw(spriteBatch);
            }
        }

        /// <summary>
        /// Updates state of the control
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Update(GameTime gameTime)
        {
            if(!Enabled)
                return;
            foreach (var control in Controls)
            {
                control.Update(gameTime);
            }
        }

        /// <summary>
        /// Handles a touch and changes the state of this component if needed
        /// </summary>
        /// <param name="touchLocation">Touch Location</param>
        /// <returns>True, if touch was handled</returns>
        public override bool ProcessTouch(TouchLocation touchLocation)
        {
            if (!Enabled)
                return false;
            return Controls.Any(control => control.ProcessTouch(touchLocation));
        }

        public override bool ProcessGesture(GestureSample gestureSample)
        {
            if (!Enabled)
                return false;
            return Controls.Any(control => control.ProcessGesture(gestureSample));
        }
        #endregion

        
    }
}
